//
//  BaseNavigationController.m
//  SleepingElf
//
//  Created by karong on 2023/6/5.
//

#import "BaseNavigationController.h"
#import "BaseController.h"

@interface BaseNavigationController ()

@end

@implementation BaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    //    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    // 开启侧邊返回手势
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = YES;
        self.interactivePopGestureRecognizer.delegate = (id)self;
    }
//    self.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //    [self.navigationController setNavigationBarHidden:YES animated:NO];
    //    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        // 禁用根目录的侧滑手势
        if (self.viewControllers.count < 2 || self.visibleViewController == [self.viewControllers objectAtIndex:0]) {
            return NO;
        }
        BaseController *vc = self.viewControllers.lastObject;
        if (vc.interactivePopDisabled) {
            return NO;
        }
    }
    return YES;
}


- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (navigationController.viewControllers.count == 1) {
        //如果是 rootViewController 禁止滑動手势
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    else{//如果不是 就启用 滑動手势
        self.interactivePopGestureRecognizer.enabled = YES;
    }
}
@end
